<?php
//require section
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_users.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_tickets.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_combos.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_coupons.php';
$title = 'Admin Page';

$id = 0;
$email = api_booking::get_default_user_email();
$ticket = api_booking::get_default_ticket();
$ticket_quantity_solve = "";
$coupon_code = api_booking::get_default_coupon_code();
$combo = api_booking::get_default_combo();
$combo_quantity_solve = "";
$action = "Add";

if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
    if (!empty($_GET['Id'])) {
        $id = $_GET['Id'];
    }
    $edit_item = api_booking::get_booking_by_id($id);
    $id = $edit_item['id'];
    $email = $edit_item['user_email'];
    $ticket = $edit_item['ticket_name'];
    $ticket_quantity_solve = $edit_item['ticket_quantity'];
    $coupon_code = $edit_item['coupon_code'];
    $combo = $edit_item['combo_name'];
    $combo_quantity_solve = $edit_item['combo_quantity'];
    $action = "Update";
} else if (!empty($_POST)) {
    $id_save = "";
    if (!empty($_POST['hidd_id'])) {
        $id_save = $_POST['hidd_id'];
    }
    $email_save = "";
    if (!empty($_POST['ddl_email'])) {
        $email_save = $_POST['ddl_email'];
    }
    $ticket_save = "";
    if (!empty($_POST['ddl_ticket'])) {
        $ticket_save = $_POST['ddl_ticket'];
    }
    $ticket_quantity_solve_save = "";
    if (!empty($_POST['txt_ticket_quantity_solve'])) {
        $ticket_quantity_solve_save = $_POST['txt_ticket_quantity_solve'];
    }
    $coupon_code_save = "";
    if (!empty($_POST['ddl_coupon_code'])) {
        $coupon_code_save = $_POST['ddl_coupon_code'];
    }
    $combo_save = "";
    if (!empty($_POST['ddl_combo'])) {
        $combo_save = $_POST['ddl_combo'];
    }
    $combo_quantity_solve_save = "";
    if (!empty($_POST['txt_combo_quantity_solve'])) {
        $combo_quantity_solve_save = $_POST['txt_combo_quantity_solve'];
    }
    if (!empty($_POST['Action']) && ($_POST['Action'] === "Add" || $_POST['Action'] === "Update")) {
        $error_show = api_booking::validate_booking_fields($email_save, $ticket_save, 
                $ticket_quantity_solve_save, $coupon_code_save, $combo_save, $combo_quantity_solve_save);
        if (empty($error_show)) {
            $ticket_id = api_booking::get_ticket_id_from_name($ticket_save);
            $user_id = api_booking::get_user_id_from_email($email_save);
            $coupon_id = api_booking::get_coupon_id_from_coupon_code($coupon_code_save);
            $combo_id = api_booking::get_combo_id_from_name($combo_save);
            if (api_booking::save_booking($id_save, $user_id, $ticket_id, 
                $ticket_quantity_solve_save, $coupon_id, $combo_id, $combo_quantity_solve_save)) {
                $success_info = "Added booking of &lt;".$email_save."&gt; user successfully.";
                if ($id_save > 0) {
                    $success_info = "Update booking of &lt;".$email_save."&gt; user successfully.";
                }
            }
        } else {
            $id = $id_save;
            $email = $email_save;
            $ticket = $ticket_save;
            $ticket_quantity_solve = $ticket_quantity_solve_save;
            $coupon_code = $coupon_code_save;
            $combo = $combo_save;
            $combo_quantity_solve = $combo_quantity_solve_save;
            $action = $_POST['Action'];
        }
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Paid") {
        $id_paid = filter_input(INPUT_POST, "hidd_paid_id");
        if (!empty($id_paid)) {
            api_booking::update_paid($id_paid);
            $booking_code = api_booking::get_booking_code($id_paid);
            // send mail to client
            $email_sent_to   = api_booking::get_user_email_from_booking_id($id_paid);
            $message         = "Now, admin activated your booking code. \n"
                             . "This is your booking code: ".$booking_code." \n"
                             . "Remember your code when you come to cinema.";
                             
            mail($email_sent_to, "Your booking code is activated", $message);
        }
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
        $id_delete = 0;
        if (!empty($_POST['hidd_delete_id'])) {
            $id_delete = $_POST['hidd_delete_id'];
        }
        api_booking::delete_booking($id_delete);
    }
}


?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="ui page grid">
            <div class="column ts-wrapper">
                <?php
                require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
                ?>
                <div class="ts-inner-wrapper">
                    <h1>Booking</h1>
                    <?php if (!empty($error_show)) { ?>
                                    <div class="ui error message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            Please correct these wrongs.
                                        </div>
                                        <ul class="list">
                                            <?php echo $error_show; ?>
                                        </ul>
                                    </div>
                                <?php } else if (!empty($success_info)) {
                                    ?>
                                <div class="ui success message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            <?php echo $success_info; ?>
                                        </div>
                                </div>
                                    <?php
                                }
                                ?>
                    <div class="hide" id="add-edit-popup">
                        <form action="booking.php" method="POST" id="form_paid" >
                            <input type="hidden" name="hidd_paid_id" value="0" />
                            <input type="hidden" name="Action" value="Paid" />
                        </form>
                        <form action="booking.php" method="POST" id="form_delete" >
                            <input type="hidden" name="hidd_delete_id" value="0" />
                            <input type="hidden" name="Action" value="Delete" />
                        </form>
                        <form name="form_data" id="form_data" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
                        <div class="ui form segment form-background">
                            
                            <input type="hidden" name="hidd_id" value="<?php echo $id; ?>" />
                            
                            <div class="field">
                                <label for="Status">User <span class="required">*</span></label>
                                <div class="ui selection dropdown">
                                    <input type="hidden" name="ddl_email" value="<?php echo $email; ?>">
                                    <div class="text"><?php echo $email; ?></div>
                                    <i class="dropdown icon"></i>
                                    <div class="menu ui transition hidden">
                                      <?php 
                                        $list = api_users::get_all_users();
                                        if (!empty($list) && count($list) > 0) {
                                            foreach ($list as $item) {?>
                                                <div class="item"><?php echo $item['email']; ?></div>
                                      <?php
                                            } 
                                        }?>
                                    </div>
                                  </div>
                            </div>
                            
                            <div class="field">
                                <label for="Status">Ticket <span class="required">*</span></label>
                                <div class="ui selection dropdown">
                                    <input type="hidden" name="ddl_ticket" value="<?php echo $ticket; ?>">
                                    <div class="text"><?php echo $ticket; ?></div>
                                    <i class="dropdown icon"></i>
                                    <div class="menu ui transition hidden">
                                      <?php 
                                        $list = api_tickets::get_all_tickets();
                                        if (!empty($list) && count($list) > 0) {
                                            foreach ($list as $item) {?>
                                                <div class="item"><?php echo $item['name']; ?></div>
                                      <?php
                                            } 
                                        }?>
                                    </div>
                                  </div>
                            </div>
                            
                            <div class="field">
                                <label for="Name">Ticket Quantity Solve <span class="required">*</span></label>
                                <input type="text" name="txt_ticket_quantity_solve" value="<?php echo $ticket_quantity_solve; ?>" placeholder="e.g: 2" />
                            </div>
                            
                            <div class="field">
                                <label for="Status">Coupon Code <span class="required">*</span></label>
                                <div class="ui selection dropdown">
                                    <input type="hidden" name="ddl_coupon_code" value="<?php echo $coupon_code; ?>">
                                    <div class="text"><?php echo $coupon_code; ?></div>
                                    <i class="dropdown icon"></i>
                                    <div class="menu ui transition hidden">
                                      <?php 
                                        $list = api_coupons::get_all_coupons();
                                        if (!empty($list) && count($list) > 0) {
                                            foreach ($list as $item) {?>
                                                <div class="item"><?php echo $item['coupon_code']; ?></div>
                                      <?php
                                            } 
                                        }?>
                                    </div>
                                  </div>
                            </div>
                            
                            <div class="field">
                                <label for="Status">Combo <span class="required">*</span></label>
                                <div class="ui selection dropdown">
                                    <input type="hidden" name="ddl_combo" value="<?php echo $combo; ?>">
                                    <div class="text"><?php echo $combo; ?></div>
                                    <i class="dropdown icon"></i>
                                    <div class="menu ui transition hidden">
                                      <?php 
                                        $list = api_combos::get_all_combos();
                                        if (!empty($list) && count($list) > 0) {
                                            foreach ($list as $item) {?>
                                                <div class="item"><?php echo $item['name']; ?></div>
                                      <?php
                                            } 
                                        }?>
                                    </div>
                                  </div>
                            </div>
                            
                            <div class="field">
                                <label for="Name">Combo Quantity Solve <span class="required">*</span></label>
                                <input type="text" name="txt_combo_quantity_solve" value="<?php echo $combo_quantity_solve; ?>" placeholder="e.g: 2" />
                            </div>
                            
                            <input type="hidden" name="Action" value="add">
                            <div class="ui buttons">
                                <a href="?"><div class="ui  button">Cancel</div></a>
                                <div class="or"></div>
                                <div id="action-button" class="ui orange button" onclick="submit_data();"><?php echo $action; ?></div>
                            </div>
                        </div>
                        </form>
                    </div>
                    
                    <h2>List</h2>
                    
                    <table class="ui table segment">
                        <thead>
                          <tr><th>User</th>
                          <th>Ticket</th>
                          <th>Ticket Quantity Solve</th>
                          <th>Coupon Code</th>
                          <th>Combo</th>
                          <th>Combo Quantity</th>
                          <th>Total Money (USD)</th>
                          <th>Paid</th>
                          <th>Actions</th>
                        </tr></thead>
                        <tbody>
                            <?php 
                            $list = api_booking::get_all_booking();
                            if (empty($list) || count($list) == 0) {
                                echo "<tr><td>No item found.</td></tr>";
                            } else {
                                $current_page = 1;
                                if (!empty($_GET['p'])) {
                                        $current_page = $_GET['p'];
                                }
                                
                                $list_total = $list;
                                $page_size = 10;
                                $lib_pager = new lib_pager($list_total, $page_size);
                                $total_page = $lib_pager->get_total_page();
                                $current_page_list = $lib_pager->get_current_page_list($current_page);

                                foreach ($current_page_list as $item) {?>
                            <tr>
                            <td><?php echo $item['user_email']; ?></td>
                            <td><?php echo $item['ticket_name']; ?></td>
                            <td><?php echo $item['ticket_quantity']; ?></td>
                            <td><?php echo $item['coupon_code']; ?></td>
                            <td><?php echo $item['combo_name']; ?></td>
                            <td><?php echo $item['combo_quantity']; ?></td>
                            <td><?php echo $item['total_money']; ?></td>
                            <?php
                            $text = "Not Yet";
                            if (!empty($item['is_paid'])) {
                                $text = "PAID";
                            }
                            ?>
                            <td><?php echo $text; ?></td>
                            <td>
                                <a href="?Action=Edit&Id=<?php echo $item['id']; ?>">
                                <i class="pencil icon"></i>
                                </a>
                                <?php
                                if (empty($item['is_paid'])) {
                                ?>
                                <a href="#" onclick="paid('<?php echo $item['id']; ?>');">
                                    <img src="<?php echo LINK_ROOT . DIR_ADMIN ?>/layout/images/pay.png" />
                                </a>
                                <?php
                                }
                                ?>
                                <a href="<?php echo LINK_ROOT . DIR_ADMIN ?>booking_lines.php?booking_id=<?php echo $item['id']; ?>&user_id=<?php echo $item['user_id']; ?>">
                                    <img src="<?php echo LINK_ROOT . DIR_ADMIN ?>/layout/images/view.png" />
                                </a>
                                <i class="remove icon" onclick="confirmDelete('<?php echo $item['id']; ?>')"></i>
                            </td>
                            <?php
                                }
                            }?>
                        </tbody>
                        <tfoot>
                          <tr><th colspan="9">
                            <div class="ui orange labeled icon button" onclick="show_popup();" ><i class="flag icon"></i> Add Booking</div>
                            <table class="paging-footer">
                                    <tbody>
                                            <tr>
                                                    <?php 
                                                    if (!empty($total_page) && count($total_page) > 0) {
                                                        for ($page = 1; $page <= $total_page; $page++) { ?>
                                                        <td>
                                                                <span>
                                                                        <a href="?p=<?php echo $page; ?>" <?php if ($current_page != $page) echo 'style="color:Black;"'; ?>><?php echo $page; ?></a>
                                                                </span>
                                                        </td>
                                                    <?php }
                                                    }?>
                                            </tr>
                                    </tbody>
                            </table>
                          </th>
                        </tr></tfoot>
                      </table>
                    
                    <script type="text/javascript">
                        <?php 
                        if ((!empty($_GET['Action']) && $_GET['Action'] === "Edit") 
                                || (!empty($_POST['Action']) && ($_POST['Action'] === "Update" || $_POST['Action'] === "Add"))) {
                            echo "show_popup();";
                        }
                        ?>
                    </script>
                </div>
                <?php
                require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
                ?>
            </div>
        </div>
    </body>
</html>